;solution for euler26

(defun decimal-part (x)
	(return-from decimal-part (- x (floor x))))

(defun find-period (d)
	(let ((r 1) (prems (vector)) mag)
		;(setf d (* d r))
		(loop 
			(loop
				(when (> (/ r d) 1) (return))
				(setf d (/ d 10)))
			(decf r (* d (floor (/ r d))))
			;(format t "~a:~a~%" d r)
			(when (= r 0) (return-from find-period 0))
			(when  (position (decimal-part (log r 10)) prems) 
				(return-from find-period 
					(- (length prems) (position (decimal-part (log r 10)) prems))))
			(setf prems (concatenate 'vector prems (vector (decimal-part (log r 10)))))
			(setf mag (expt 10 (- (floor (log r 10)))))
			(setf r (* r mag))
			(setf d (* d mag)))))

(defun solve-euler26 ()
	(let ((mp 0) cp d)
		(loop for i from 1 to 999 do
			(setf cp (find-period i))
			(when (> cp mp) (setf mp cp) (setf d i)))
		(format t "Euler 26: ~a" d)))